package com.shundr.common.view;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shundr.R;
import com.shundr.common.dao.AttributeDao;
import com.shundr.common.model.AttributeInfo;

@SuppressLint("InflateParams")
public class PopSelectArea extends PopupWindow{
	private Context mContext;
	public GridView mGridView;
	/**
	 * 筛选数据 适配器 list
	 */
	private List<AttributeInfo> mList = new ArrayList<AttributeInfo>();
	
	private OnItemSelectedListener selectedListener;
	
	private String mType;

	private ScreeingAdapter mAdapter;

	public PopSelectArea(Context context,String type,OnItemSelectedListener selectedListener) {
		super(context);
		this.mContext = context;
		this.mType = type;
		this.selectedListener=selectedListener;
		initPopupWindowView();
	}

	@SuppressWarnings("deprecation")
	private void initPopupWindowView() {
		LayoutInflater layoutInflater = LayoutInflater.from(mContext);
		View view = layoutInflater.inflate(R.layout.view_screeing_popup_window, null);
		view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		mGridView = (GridView) view.findViewById(R.id.carinfo_gridview_k);
		mList.addAll(new AttributeDao().getAttrByType(mType));
		mAdapter = new ScreeingAdapter();
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					for(AttributeInfo info : mList){
						info.setSelected(false);
					}
					mList.get(position).setSelected(true);
					mAdapter.notifyDataSetChanged();
					selectedListener.onClick(mList.get(position));
				} catch (Exception e) {
					e.printStackTrace();
				}				
				selectedListener.onPopupWindowDismiss();
				dismiss();
			}
		});
		
		setContentView(view);
		// 设置动画效果 [R.style.AnimationFade]
		this.setAnimationStyle(R.style.mypopwindow_anim_style);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.MATCH_PARENT);
		this.setFocusable(true);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setOutsideTouchable(true);
	}
	
	public void showPopupWindow(View v) {
		selectedListener.onPopupWindowShow();
		showAsDropDown(v);
	}

	class ScreeingAdapter extends BaseAdapter {
		LayoutInflater inflater;
		ViewHold viewhold = null;

		public ScreeingAdapter() {
			super();
			inflater = LayoutInflater.from(mContext);
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			viewhold = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.view_screeing_gridview_item, null);
				viewhold = new ViewHold();
				viewhold.itemText = (TextView) convertView.findViewById(R.id.text);
				viewhold.ivSelected = (ImageView) convertView.findViewById(R.id.iv_selected);
				convertView.setTag(viewhold);
			} else {
				viewhold = (ViewHold) convertView.getTag();
			}
			try {
				AttributeInfo info = mList.get(position);
				viewhold.itemText.setText(info.getName());
				if(info.isSelected()){
					viewhold.ivSelected.setVisibility(View.VISIBLE);
				}else{
					viewhold.ivSelected.setVisibility(View.GONE);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHold {
			TextView itemText;
			ImageView ivSelected;
		}
	}
	
	

	@Override
	public void dismiss() {
		selectedListener.onPopupWindowDismiss();
		super.dismiss();
	}
	
	public interface OnItemSelectedListener {
		public void onClick(AttributeInfo info);
		public void onPopupWindowDismiss();
		public void onPopupWindowShow();
	}
}
